<?php
//公共函数


//获取网站信息
function site($domain){
	ob_implicit_flush();
	$domain	= substr($domain,0,7)=="http://"?$domain:"http://" . $domain;
	$Start=microtime(true);
	$pagecode=curl_file_get_contents($domain);
	$Finish=microtime(true);
	$site["opentime"]		= number_format($Finish-$Start,4);		//网页打开时间
	//判断编码进行转码
	if(strpos(strtolower($pagecode),"charset=gb2312")>0){
		$pagecode=mb_convert_encoding($pagecode,"UTF-8","gb2312");
	}
	if(strpos(strtolower($pagecode),"<title>301 moved permanently</title>")>0){
		//提取301转跳网址
		$domain		= getBySpliti("<a href=\"","\">",$pagecode);
		return site($domain);
	}

	//提取网页标题
	$site["title"]		= getBySpliti("<title>","</title>",$pagecode);
	//提取网页描述
	$site["description"]		= getBySpliti("description\" content=\"","\"",$pagecode);
	//提取网页关键字
	$site["keyword"]		= getBySpliti("keywords\" content=\"","\"",$pagecode);
	$site["robot"]			= deletehtml($pagecode);

	//提取网页内链与外链
	$LinkCount1=0;
	$LinkCount2=0;
	$Arr=spliti("<a",$pagecode);
	for($i=1;$i<count($Arr)-1;$i++){
		$Arr2=spliti("</a>",$Arr[$i]);
		if(strpos($Arr2[0],$domain)!=""||strpos($Arr2[0],"http://")==""){
			$LinkCount1=$LinkCount1+1;
		}else{
			$LinkCount2=$LinkCount2+1;
		}
	}
	$site["linkin"]				= $LinkCount1;
	$site["linkout"]			= $LinkCount2;


	//提取网页图片
	$ImageCount=0;
	$ImageAltCount=0;
	$Arr=spliti("<img",$pagecode);
	$ImageCount=count($Arr)-1;
	for($i=1;$i<count($Arr)-1;$i++){
		$Arr2=spliti(">",$Arr[$i]);
		if(strpos($Arr2[0],"alt=")!=""){
			$ImageAltCount=$ImageAltCount+1;
		}
	}
	$site["image"]				= $ImageCount;
	$site["imagealt"]			= $ImageAltCount;

	return $site;
}

function getBySpliti($start,$end,$code){
	$Arr=spliti($start,$code);
	$Arr=spliti($end,$Arr[1]);
	return $Arr[0];
}

//删除html
function deletehtml($str){
	$str = trim($str);
	$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
	$str = strip_tags($str,"");
	$str = ereg_replace("\t","",$str);
	$str = ereg_replace("\r\n","",$str);
	$str = ereg_replace("\r","",$str);
	$str = ereg_replace("\n","",$str);
	$str = ereg_replace("&nbsp;"," ",$str);
	$str = ereg_replace("  "," ",$str);
	return trim($str);
}


//获取ALEXA信息
function alexa($domain){
	ob_implicit_flush();
	$fp = fsockopen("www.alexa.com", 80, $errno, $errstr, 60);
	if(!$fp){
		$alexa["error"]	= "$errstr ($errno)<br />\n";
	}else{
		$out = "GET /siteinfo/".str_replace("www.","",$domain)." HTTP/1.1\r\n" ;
		$out .= "Host: www.alexa.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$pagecode="";
		while (!feof($fp)) {
			$pagecode.= fgets($fp, 128);
		}
		fclose($fp);
	}
	//获取排名
	$Arr=spliti("<td class=\"small\" align=\"center\">Traffic Rank</td>",$pagecode);
	$Arr=spliti("</table>",$Arr[1]);
	$AlexaData=$Arr[0];
	$Arr=spliti("<th>7 day</th>",$AlexaData);
	$Arr=spliti("</td>",$Arr[1]);
	$Arr=spliti(">",$Arr[0]);
	$AlexaDay=$Arr[1];
	$Arr=spliti("<th>1 month</th>",$AlexaData);
	$Arr=spliti("</td>",$Arr[1]);
	$Arr=spliti(">",$Arr[0]);
	$AlexaMonth1=$Arr[1];
	$Arr=spliti("<th>3 month</th>",$AlexaData);
	$Arr=spliti("</td>",$Arr[1]);
	$Arr=spliti(">",$Arr[0]);
	$AlexaMonth3=$Arr[1];
	//排名输出
	//$alexa["alexaday"]		= $AlexaDay;
	$alexa["alexaweek"]		= str_replace(",","",$AlexaDay);
	$alexa["alxeamonth"]		= str_replace(",","",$AlexaMonth1);
	$alexa["alxeamonth3"]	= str_replace(",","",$AlexaMonth3);
	//获取IP、PV
	$Arr=spliti("<td class=\"small\" align=\"center\">Reach</td>",$pagecode);
	$Arr=spliti("</table>",$Arr[1]);
	$AlexaData=$Arr[0];
	$Arr=spliti("<th>1 month</th>",$AlexaData);
	$Arr=spliti("</td>",$Arr[1]);
	$Arr=spliti(">",$Arr[0]);
	$AlexaIp=$Arr[1];
	$AlexaIp=$AlexaIp*10000*600;
	$Arr=spliti("<td class=\"small\" align=\"center\">Pageviews/User</td>",$pagecode);
	$Arr=spliti("</table>",$Arr[1]);
	$AlexaData=$Arr[0];
	$Arr=spliti("<th>1 month</th>",$AlexaData);
	$Arr=spliti("</td>",$Arr[1]);
	$Arr=spliti(">",$Arr[0]);
	$AlexaPv=$Arr[1];
	$AlexaPv=$AlexaIp*$AlexaPv;
	$alexa["alexaip"]		= $AlexaIp;
	$alexa["alexapv"]		= $AlexaPv;
	return $alexa;
}

//获取服务器版本
function GetServerInfo($url){
	$url	= substr($url,0,7)=="http://"?$url:"http://" . $url;
	$url = parse_url($url);
	$fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error);
	if($fp){
			fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
			fputs($fp,"Host:$url[host]\r\n\r\n");
			$tmp="";
			while(!$end){
					$line = fgets($fp,2048);
					if(trim($line)=="")
							$end=true;
					else
							$tmp=$tmp.$line."<br />";
			}
			return $tmp;
	}else{
			return "";
	}
}

//获取域名IP
function GetDomainIp($url){
	$url	= strtolower($url);
	$url	= substr($url,0,7)=="http://"?str_replace("http://","",$url):$url;
	return gethostbyname($url);
}


//封装curl获取数据
function curl_file_get_contents($durl){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $durl);
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
   curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
   curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $r = curl_exec($ch);
   curl_close($ch);
   return $r;
 }

//获取详细Whois信息
function whois($domain){
	$domain	= get_domain($domain);
	$domain	= "http://whois.domaintools.com/" . $domain;
	echo $domain;
	//exit;
	//$pagecode=curl_file_get_contents($domain);
	echo $pagecode;
}








function GetWhois($url){
	$fp = fsockopen("whois.ename.net", 80, $errno, $errstr, 60);
	if(!$fp){
		$whois["error"]	= "$errstr ($errno)<br />\n";
	}else{
		$out = "GET /".$url."_whois.dns.com.cn HTTP/1.1\r\n" ;
		$out .= "Host: whois.ename.net\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);

		$Arr=spliti("<td style=\"border-bottom:1px dashed #C2DBF0;height:30px;width:615px;\">",$data);
		$Arr=spliti("</td></tr>",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}














//采集网址
function geturl($url){
	$code = file_get_contents($url);
	preg_match_all("/<a[^>]*?\s*href=\"(.*?)\"(.*?)>(.*?)<\/a>/i",$code,$infos);
	$infos = $infos[1];
	$infos = array_unique($infos);
	foreach($infos as $v){
		$domain	= get_domain($v);
		if($domain){
			$urlarr[]	= $domain;
		}
	}
	$urlarr = array_unique($urlarr);
	return $urlarr;
}

//获取域名
// url=http://www.laxiao.com/com/area/123456.html => laxiao.com
function get_domain($url){
	$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/";
	preg_match($pattern, $url, $matches);
	if(count($matches) > 0) {
		return $matches[0];
	}else{
		$rs = parse_url($url);
		$main_url = $rs["host"];
		if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
			return $main_url;
		}elseif(empty($main_url)){
			return false;
		}else{
			$arr = explode(".",$main_url);
			$count=count($arr);
			$endArr = array("com","net","org","3322");//com.cn net.cn 等情况
			if (in_array($arr[$count-2],$endArr)){
				$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
			}else{
				$domain = $arr[$count-2].".".$arr[$count-1];
			}
			return $domain;
		}
	}
}










	function zeroFill($a, $b){
		$z = hexdec(80000000);
		if ($z & $a){
			$a = ($a>>1);
			$a &= (~$z);
			$a |= 0x40000000;
			$a = ($a>>($b-1));
		}else{
			$a = ($a>>$b);
		}
		return $a;
	}
	function mix($a,$b,$c){
		$a -= $b; $a -= $c; $a ^= (zeroFill($c,13));
		$b -= $c; $b -= $a; $b ^= ($a<<8);
		$c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
		$a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
		$b -= $c; $b -= $a; $b ^= ($a<<16);
		$c -= $a; $c -= $b; $c ^= (zeroFill($b,5));
		$a -= $b; $a -= $c; $a ^= (zeroFill($c,3));
		$b -= $c; $b -= $a; $b ^= ($a<<10);
		$c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
		return array($a,$b,$c);
	}
	function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC){
		if(is_null($length)) {
			$length = sizeof($url);
		}
		$a = $b = 0x9E3779B9;
		$c = $init;
		$k = 0;
		$len = $length;
		while($len >= 12) {
			$a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
			$b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
			$c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
			$mix = mix($a,$b,$c);
			$a = $mix[0]; $b = $mix[1]; $c = $mix[2];
			$k += 12;
			$len -= 12;
		}
		$c += $length;
		switch($len){
			case 11: $c+=($url[$k+10]<<24);
			case 10: $c+=($url[$k+9]<<16);
			case 9 : $c+=($url[$k+8]<<8);
			/* the first byte of c is reserved for the length */
			case 8 : $b+=($url[$k+7]<<24);
			case 7 : $b+=($url[$k+6]<<16);
			case 6 : $b+=($url[$k+5]<<8);
			case 5 : $b+=($url[$k+4]);
			case 4 : $a+=($url[$k+3]<<24);
			case 3 : $a+=($url[$k+2]<<16);
			case 2 : $a+=($url[$k+1]<<8);
			case 1 : $a+=($url[$k+0]);
		}
		$mix = mix($a,$b,$c);
		/* report the result */
		return $mix[2];
	}
	function strord($string){
		for($i=0;$i<strlen($string);$i++) {
			$result[$i] = ord($string{$i});
		}
		return $result;
	}
	function pagerank($url){
		$ch = "6".GoogleCH(strord("info:" . $url));
		$fp = fsockopen("www.google.com", 80, $errno, $errstr, 60);
		if(!$fp){
			echo "$errstr ($errno)<br />\n";
		}else{
			$out = "GET /search?client=navclient-auto&ch=" . $ch .  "&features=Rank&q=info:" . $url . " HTTP/1.1\r\n" ;
			$out .= "Host: www.google.com\r\n" ;
			$out .= "Connection: Close\r\n\r\n" ;
			fwrite($fp, $out);
			while (!feof($fp)) {
				$data = fgets($fp, 128);
				$pos = strpos($data, "Rank_");
				if($pos === false){
				}else{
					$pagerank = substr($data, $pos + 9);
					fclose($fp);
					return $pagerank;
				}
			}
			fclose($fp);
			return 0;
		}
	}
	function printranks($domains){
		foreach($domains as $value){
			$elements[] = printrank($value);
		}
		return $elements;
	}













//获取百度收录
function GetBaidu($url,$type){
		if($type==0){
			$url = "http://www.baidu.com/s?wd=site%3A".$url."";
		}else{
			$url = "http://www.baidu.com/s?wd=link%3A".$url."";
		}
		$data=curl_file_get_contents($url);
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("找到相关结果",$data);
		$Arr=spliti("个",$Arr[1]);
		if($Arr[0]!=""){
			$return	= str_replace(",","",$Arr[0]);
			$return	= str_replace("约","",$return);
			return $return;
		}else{
			return "0";
		}
}

//获取谷歌收录
function GetGoogle($url,$type){
		if($type==0){
			$url = "http://www.google.com/search?hl=en&q=site%3A".$url."";
		}else{
			$url = "http://www.google.com/search?hl=en&q=link%3A".$url."";
		}
		$data=curl_file_get_contents($url);
		//$data=mb_convert_encoding($data,"UTF-8","gb2312");
		if($type==0){
			$Arr=spliti("About",$data);
		}else{
			$Arr=spliti("<div id=resultstats>",$data);
		}
		$Arr=spliti("results",$Arr[1]);
		if($Arr[0]!="")
			return str_replace(",","",$Arr[0]);
		else
			return "0";
}

//获取微软BING收录
function GetBing($url,$type){
	$fp = fsockopen("www.bing.com", 80, $errno, $errstr, 60);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.bing.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		//echo $data;
		fclose($fp);
		$Arr=spliti("条结果，共 ",$data);
		$Arr=spliti(" 条",$Arr[1]);
		if($Arr[0]!="")
			return str_replace(",","",$Arr[0]);
		else
			return "0";
	}
}
//获取雅虎收录
function GetYahoo($url,$type){
		$url = "http://siteexplorer.search.yahoo.com/search?p=".$url."";
		$data=curl_file_get_contents($url);
		//echo $data;
		if($type==0){
			$Arr=spliti(">pages",$data);
			$Arr=spliti(")",$Arr[1]);
		}else{
			$Arr=spliti('>inlinks',$data);
			$Arr=spliti(')',$Arr[1]);
		}
		if($Arr[0]!=""){
			$return		= str_replace(",","",$Arr[0]);
			$return		= str_replace("(","",$return);
			return $return;
		}else{
			return "0";
		}
}
//获取搜搜收录
function GetSoso($url,$type){
	$fp = fsockopen("www.soso.com", 80, $errno, $errstr, 60);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /q?w=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /q?w=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.soso.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("搜索到约",$data);
		$Arr=spliti("项结果",$Arr[1]);
		if($Arr[0]!="")
			return str_replace(",","",$Arr[0]);
		else
			return "0";
	}
}
//获取有道收录
function GetYoudao($url,$type){
	$fp = fsockopen("www.youdao.com", 80, $errno, $errstr, 60);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.youdao.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$Arr=spliti("<span class=\"fr\">共",$data);
		$Arr=spliti("条结果",$Arr[1]);
		if($Arr[0]!="")
			return str_replace(",","",$Arr[0]);
		else
			return "0";
	}
}
//获取搜狗收录
function GetSogou($url,$type){
	$fp = fsockopen("www.sogou.com", 80, $errno, $errstr, 60);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /web?query=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /web?query=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.sogou.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		if($type==3){
			$Arr=spliti("搜狗评级:",$data);
			$Arr=spliti("&nbsp;",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "0";
		}else{
			$Arr=spliti("<!--resultbarnum:",$data);
			$Arr=spliti("-->",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "0";
		}
	}
}

//获取sogouPR值
function GetSogouPR($url){
	$fp = fsockopen("rank.ie.sogou.com", 80, $errno, $errstr, 60);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		$out = "GET /sogourank.php?ur=http%3A%2F%2F".$url."%2F HTTP/1.1\r\n" ;
		$out .= "Host: rank.ie.sogou.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ;
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("sogourank=",$data);
		$Arr=spliti("\n",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "0";
	}
}


?>
